package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysResourceDAO;
import com.ourteam.system.dao.SysResource;
import com.ourteam.system.dao.SysResourceDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:34
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysResourceDAOJdbcImpl extends AbstractSpringDAO
    implements ISysResourceDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, DomainId, ParentResourceId, Remarks, ResourceCode, ResourceName,
            ResourceTypeId, Status, Sort, ResourceTitleKey, DomainTitleKey
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.DomainId,
            _DataObjectProperty.ParentResourceId, _DataObjectProperty.Remarks,
            _DataObjectProperty.ResourceCode, _DataObjectProperty.ResourceName,
            _DataObjectProperty.ResourceTypeId, _DataObjectProperty.Status,
            _DataObjectProperty.Sort, _DataObjectProperty.ResourceTitleKey,
            _DataObjectProperty.DomainTitleKey
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysResource, SysResourceDAOQueryBean> daoHelper;
    private com.ourteam.system.dao.impl.AbstractSysResourcePropertyDAOJdbcImpl SysResourcePropertyDao;
    private com.ourteam.system.dao.impl.AbstractSysResourceTypeDAOJdbcImpl SysResourceTypeDao;

    public AbstractSysResourceDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysResource, SysResourceDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysDomainDAO.EntityName)
                                .append(" resourceDomain ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysDomainDAO.Id,
                    com.ourteam.system.dao.ISysDomainDAO.EntityName + ".",
                    "resourceDomain."));
            relatedJoinSQLBuffer.append(" = ").append(DomainId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysResource
         * @throws Exception
         */
    public boolean checkIsUnique(SysResource aSysResource)
        throws Exception {
        if (aSysResource == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysResource.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysResource
     * @throws Exception
     */
    public void insert(SysResource aSysResource) throws Exception {
        super.doInsert(aSysResource);
    }

    /**
         * Batch Insert Datas
         * @param aSysResource
         * @throws Exception
         */
    public void batchInsert(SysResource[] aSysResources)
        throws Exception {
        super.doBatchInsert(aSysResources);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysResourceDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(SysResource aSysResource) throws Exception {
        delete(aSysResource.getId());
    }

    /**
     * Update Data Selective
     * @param aSysResource
     * @throws Exception
     */
    public void update(SysResource aSysResource) throws Exception {
        if (aSysResource.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysResource);

        SysResource tempObj = findById(aSysResource.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysResource, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysResources[]
     * @throws Exception
     */
    public void batchUpdate(SysResource[] aSysResources)
        throws Exception {
        super.doBatchUpdate(aSysResources);
    }

    /**
         * Update Data By Condictions
         * @param aSysResource
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysResource aSysResource,
        SysResourceDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aSysResource, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysResourceDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(SysResource aSysResource)
        throws Exception {
        if (aSysResource.getRemarks() == null) {
            throw new Exception(
                "The SysResource.remarks value is not allow NULL");
        }

        if (aSysResource.getResourceName() == null) {
            throw new Exception(
                "The SysResource.resourceName value is not allow NULL");
        }

        if (aSysResource.getStatus() == null) {
            throw new Exception(
                "The SysResource.status value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysResource findById(final long id) throws Exception {
        SysResourceDAOQueryBean queryBean = new SysResourceDAOQueryBean();

        SysResourceDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysResource[] SysResources = query(queryBean);

        if (SysResources.length == 0) {
            return null;
        } else {
            return SysResources[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysResource[]
         * @throws Exception
         */
    public SysResource[] query(SysResourceDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean, new JdbcSysResourceRowMapper());

        return (SysResource[]) result.toArray(new SysResource[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        SysResourceDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysResourceDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(SysResource[] aSysResources,
        SysResourceDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aSysResources, queryBean);
    }

    public void synchronize(SysResource[] aSysResources,
        SysResourceDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysResource> callBack)
        throws Exception {
        daoHelper.synchronize(aSysResources, queryBean, callBack);
    }

    public void setSysResourcePropertyDao(
        com.ourteam.system.dao.ISysResourcePropertyDAO SysResourcePropertyDao) {
        this.SysResourcePropertyDao = (com.ourteam.system.dao.impl.AbstractSysResourcePropertyDAOJdbcImpl) SysResourcePropertyDao;
    }

    /**
     * Query Related SysResourceProperty
    * @param queryBean
     * @throws Exception
     */
    public com.ourteam.system.dao.SysResourceProperty[] querySysResourceProperty(
        SysResourceDAOQueryBean queryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysResourcePropertyDAO.SysResourceId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysResourcePropertyDAO.EntityName +
            " ref_SysResourceProperty";

        String[] entityFields = com.ourteam.system.dao.impl.SysResourcePropertyDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysResourcePropertyDAO.ENTITY_PROPERTIES;

        List list = SysResourcePropertyDao.doQuery(entityName, entityFields,
                entityProperties, queryBean, joinSQL.toString(),
                new com.ourteam.system.dao.impl.SysResourcePropertyDAO.JdbcSysResourcePropertyRowMapper());

        return (com.ourteam.system.dao.SysResourceProperty[]) list.toArray(new com.ourteam.system.dao.SysResourceProperty[list.size()]);
    }

    /**
     * Query Related SysResourceProperty Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySysResourcePropertySelective(
        SysResourceDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyDAO.Id,
                "id");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyDAO.Remarks,
                "remarks");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyDAO.ResourcePropertyCode,
                "resourcePropertyCode");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyDAO.ResourcePropertyDefaultValue,
                "resourcePropertyDefaultValue");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyDAO.ResourcePropertyName,
                "resourcePropertyName");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyDAO.ResourcePropertyOrder,
                "resourcePropertyOrder");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyDAO.ResourcePropertyType,
                "resourcePropertyType");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyDAO.ResourcePropertyValue,
                "resourcePropertyValue");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyDAO.Status,
                "status");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyDAO.SysResourceId,
                "sysResourceId");
        }

        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysResourcePropertyDAO.SysResourceId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysResourcePropertyDAO.EntityName +
            " ref_SysResourceProperty";

        return SysResourcePropertyDao.doQuerySelective(entityName, queryBean,
            joinSQL.toString());
    }

    /**
     * Update Related SysResourceProperty
     * @param aSysResourceProperty
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void updateSysResourceProperty(
        com.ourteam.system.dao.SysResourceProperty aSysResourceProperty,
        SysResourceDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysResourcePropertyDAO.SysResourceId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysResourcePropertyDAO.EntityName +
            " ref_SysResourceProperty";

        String[] entityFields = com.ourteam.system.dao.impl.SysResourcePropertyDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysResourcePropertyDAO.ENTITY_PROPERTIES;

        SysResourcePropertyDao.doUpdateByQuery(entityName, entityFields,
            entityProperties, joinSQL.toString(), aSysResourceProperty,
            aQueryBean);
    }

    /**
     * Delete Related SysResourceProperty
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void deleteSysResourceProperty(SysResourceDAOQueryBean aQueryBean)
        throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysResourcePropertyDAO.SysResourceId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysResourcePropertyDAO.EntityName; // + " ref_SysResourceProperty";

        SysResourcePropertyDao.doDeleteByQuery(entityName, joinSQL.toString(),
            aQueryBean);
    }

    public void setSysResourceTypeDao(
        com.ourteam.system.dao.ISysResourceTypeDAO SysResourceTypeDao) {
        this.SysResourceTypeDao = (com.ourteam.system.dao.impl.AbstractSysResourceTypeDAOJdbcImpl) SysResourceTypeDao;
    }

    /**
     * Query Related SysResourceType
    * @param queryBean
     * @throws Exception
     */
    public com.ourteam.system.dao.SysResourceType[] querySysResourceType(
        SysResourceDAOQueryBean queryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysResourceTypeDAO.ResourceTypeId)
               .append(" = ").append(ResourceTypeId);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysResourceTypeDAO.EntityName +
            " ref_SysResourceType";

        String[] entityFields = com.ourteam.system.dao.impl.SysResourceTypeDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysResourceTypeDAO.ENTITY_PROPERTIES;

        List list = SysResourceTypeDao.doQuery(entityName, entityFields,
                entityProperties, queryBean, joinSQL.toString(),
                new com.ourteam.system.dao.impl.SysResourceTypeDAO.JdbcSysResourceTypeRowMapper());

        return (com.ourteam.system.dao.SysResourceType[]) list.toArray(new com.ourteam.system.dao.SysResourceType[list.size()]);
    }

    /**
     * Query Related SysResourceType Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySysResourceTypeSelective(
        SysResourceDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourceTypeDAO.Id,
                "id");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourceTypeDAO.ConfigCode,
                "configCode");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourceTypeDAO.ConfigName,
                "configName");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourceTypeDAO.DomainId,
                "domainId");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourceTypeDAO.Remarks,
                "remarks");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourceTypeDAO.Status,
                "status");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourceTypeDAO.ResourceTypeId,
                "resourceTypeId");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourceTypeDAO.Sort,
                "sort");
        }

        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysResourceTypeDAO.ResourceTypeId)
               .append(" = ").append(ResourceTypeId);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysResourceTypeDAO.EntityName +
            " ref_SysResourceType";

        return SysResourceTypeDao.doQuerySelective(entityName, queryBean,
            joinSQL.toString());
    }

    /**
     * Update Related SysResourceType
     * @param aSysResourceType
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void updateSysResourceType(
        com.ourteam.system.dao.SysResourceType aSysResourceType,
        SysResourceDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysResourceTypeDAO.ResourceTypeId)
               .append(" = ").append(ResourceTypeId);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysResourceTypeDAO.EntityName +
            " ref_SysResourceType";

        String[] entityFields = com.ourteam.system.dao.impl.SysResourceTypeDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysResourceTypeDAO.ENTITY_PROPERTIES;

        SysResourceTypeDao.doUpdateByQuery(entityName, entityFields,
            entityProperties, joinSQL.toString(), aSysResourceType, aQueryBean);
    }

    /**
     * Delete Related SysResourceType
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void deleteSysResourceType(SysResourceDAOQueryBean aQueryBean)
        throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysResourceTypeDAO.ResourceTypeId)
               .append(" = ").append(ResourceTypeId);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysResourceTypeDAO.EntityName; // + " ref_SysResourceType";

        SysResourceTypeDao.doDeleteByQuery(entityName, joinSQL.toString(),
            aQueryBean);
    }

    public static class JdbcSysResourceRowMapper implements RowMapper<SysResource> {
        public SysResource mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysResource object = new SysResource();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object domainId = null;

                domainId = resultSet.getObject(columnIndex++);

                object.setDomainIdNull(resultSet.getBoolean(columnIndex++));

                if (domainId != null) {
                    setBeanValue(object, _DataObjectProperty.DomainId, domainId);
                }

                Object parentResourceId = null;

                parentResourceId = resultSet.getObject(columnIndex++);

                object.setParentResourceIdNull(resultSet.getBoolean(
                        columnIndex++));

                if (parentResourceId != null) {
                    setBeanValue(object, _DataObjectProperty.ParentResourceId,
                        parentResourceId);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object resourceCode = null;

                resourceCode = resultSet.getObject(columnIndex++);

                object.setResourceCodeNull(resultSet.getBoolean(columnIndex++));

                if (resourceCode != null) {
                    setBeanValue(object, _DataObjectProperty.ResourceCode,
                        resourceCode);
                }

                Object resourceName = null;

                resourceName = resultSet.getObject(columnIndex++);

                object.setResourceNameNull(resultSet.getBoolean(columnIndex++));

                if (resourceName != null) {
                    setBeanValue(object, _DataObjectProperty.ResourceName,
                        resourceName);
                }

                Object resourceTypeId = null;

                resourceTypeId = resultSet.getObject(columnIndex++);

                object.setResourceTypeIdNull(resultSet.getBoolean(columnIndex++));

                if (resourceTypeId != null) {
                    setBeanValue(object, _DataObjectProperty.ResourceTypeId,
                        resourceTypeId);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object sort = null;

                sort = resultSet.getObject(columnIndex++);

                object.setSortNull(resultSet.getBoolean(columnIndex++));

                if (sort != null) {
                    setBeanValue(object, _DataObjectProperty.Sort, sort);
                }

                Object resourceTitleKey = null;

                resourceTitleKey = resultSet.getObject(columnIndex++);

                object.setResourceTitleKeyNull(resultSet.getBoolean(
                        columnIndex++));

                if (resourceTitleKey != null) {
                    setBeanValue(object, _DataObjectProperty.ResourceTitleKey,
                        resourceTitleKey);
                }

                Object domainTitleKey = null;

                domainTitleKey = resultSet.getObject(columnIndex++);

                object.setDomainTitleKeyNull(resultSet.getBoolean(columnIndex++));

                if (domainTitleKey != null) {
                    setBeanValue(object, _DataObjectProperty.DomainTitleKey,
                        domainTitleKey);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
